/*
 * Copyright (C) 2009 codemobiles.com.
 * http://www.codemobiles.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * author: Chaiyasit Tayabovorn 
 * email: chaiyasit.t@gmail.com
 */

package com.codemobiles.droidslator_tattoo.manager;

import java.util.Hashtable;

import android.content.Context;
import android.widget.ArrayAdapter;
import com.google.api.translate.Language;

public class LanguageMgr {
  private ArrayAdapter<String> adapterLang;
  private Hashtable<String, String> langHashNameCode = new Hashtable<String, String>();
  private Hashtable<String, String> langHashCodeName = new Hashtable<String, String>();

  public LanguageMgr(Context context) {
    setAdapterLang(new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, getLangList("")));
    langHashNameCode = getLangHash();
    langHashCodeName = getLangByCodeHash();
  }

  public LanguageMgr(Context context, String filterChars) {
    setAdapterLang(new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, getLangList(filterChars)));
    langHashNameCode = getLangHash();
    langHashCodeName = getLangByCodeHash();
  }

  /**
   * Return language name by input language code
   * 
   * @return
   */
  public String getLangNameByCode(String langCode) {
    String ret;
    String langName;
    if (langCode.length() > 2) {
      langCode = langCode.substring(0, 2) + "-" + langCode.substring(3, 5).toUpperCase();
    }
    langName = langHashCodeName.get(langCode);
    if (langName == null)
      ret = "";
    else
      ret = langName;
    return ret;
  }

  /**
   * Return language name by input language code
   * 
   * @return
   */
  public String getLangCodeByName(String langName) {
    String ret;
    String langCode = langHashNameCode.get(langName);
    if (langCode == null)
      ret = "";
    else
      ret = langCode.toString().replace('-', '_').toLowerCase();
    return ret;
  }

  /**
   * Return list of all available languages can be translated.
   * 
   * @return
   */
  private Hashtable<String, String> getLangHash() {
    Language[] langs = Language.values();
    Hashtable<String, String> langMap = new Hashtable<String, String>();
    final int langNum = langs.length;
    for (int i = 0; i < langNum; i++) {
      langMap.put(langs[i].name(), langs[i].toString());
    }
    return langMap;
  }

  /**
   * Return list of all available languages can be translated by key is language
   * code.
   * 
   * @return
   */
  private Hashtable<String, String> getLangByCodeHash() {
    Language[] langs = Language.values();
    Hashtable<String, String> langMap = new Hashtable<String, String>();
    final int langNum = langs.length;
    for (int i = 0; i < langNum; i++) {
      if (i + 1 > getAdapterLang().getCount())
        continue;
      langMap.put(langs[i].toString(), langs[i].name());
    }
    return langMap;
  }

  /**
   * Return list of all available languages can be translated.
   * 
   * @return
   */
  public String[] getLangList(final String filterChars) {
    Language[] langs = Language.values();
    final int langNum = langs.length;
    int lenOfLang = 0;
    int lenOfFilter = 0;
    int focusLangNum = 0;
    for (int i = 0; i < langNum; i++) {
      lenOfLang = langs[i].name().length();
      lenOfFilter = filterChars.length();
      if (lenOfFilter <= lenOfLang) {
        if (filterChars.equalsIgnoreCase("")
            || langs[i].name().substring(0, filterChars.length()).equalsIgnoreCase(filterChars)) {
          focusLangNum++;
        }
      }
    }

    String[] langStrArray = new String[focusLangNum];
    int j = 0;

    for (int i = 0; i < langNum && focusLangNum > 0; i++) {
      lenOfLang = langs[i].name().length();
      lenOfFilter = filterChars.length();
      if (lenOfFilter <= lenOfLang) {
        if (filterChars.equalsIgnoreCase("")
            || langs[i].name().substring(0, filterChars.length()).equalsIgnoreCase(filterChars)) {
          langStrArray[j] = langs[i].name();
          j++;
        }
      }
    }
    return langStrArray;
  }

  /**
   * Convert flag name from long format to short format
   * 
   * @return
   */
  public String convertNameToShort(String name) {
    int pos = name.indexOf("_");
    if (pos == -1)
      return name;

    String langName;
    langName = name.substring(0, pos + 1) + name.substring(pos + 1, pos + 3).toUpperCase();
    return langName;
  }

  private void setAdapterLang(ArrayAdapter<String> adapterLang) {
    this.adapterLang = adapterLang;
  }

  public ArrayAdapter<String> getAdapterLang() {
    return adapterLang;
  }
}